package com.jingdianjichi.auth.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.convert.AuthRoleDTOConverter;
import com.jingdianjichi.auth.convert.AuthUserDTOConverter;
import com.jingdianjichi.auth.domain.convert.AuthRoleConverter;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import com.jingdianjichi.auth.dto.AuthRoleDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/role/")
public class RoleController {

    @Resource
    AuthRoleDomainService authRoleDomainService;
    @RequestMapping("add")
    public Result<Boolean> register(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(authRoleDTO.getRoleName()));
            Preconditions.checkArgument(!StringUtils.isEmpty(authRoleDTO.getRoleKey()));
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBoAuthRole(authRoleDTO);
            return Result.ok(authRoleDomainService.add(authRoleBO));
        } catch (Exception e) {
            return Result.fail("添加角色失败");
        }
    }
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(authRoleDTO.getRoleName()));
            Preconditions.checkArgument(!StringUtils.isEmpty(authRoleDTO.getRoleKey()));
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBoAuthRole(authRoleDTO);
            return Result.ok(authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            return Result.fail("更新角色失败");
        }
    }

    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(authRoleDTO));
            }
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBoAuthRole(authRoleDTO);
            return Result.ok(authRoleDomainService.delete(authRoleBO));
        } catch (Exception e) {
            return Result.fail("更新角色失败");
        }
    }
}
